package com.astrofizzbizz.stoneedgetunnel.keymaster;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.astrofizzbizz.utilities.DpmSwingUtilities;
import com.astrofizzbizz.utilities.FileReaderUtilities;


public class GmailLoginDialog extends JDialog
{
	private static final long serialVersionUID = -2153159339179283075L;

	KeyMasterGui keyMasterGui;
	private String coordinatorGmailLogin = null;
	private String coordinatorName = null;
	private String coordinatorGmailPassword = null;
	private JTextField coordinatorNameField     = new JTextField(25);
	private JTextField coordinatorGmailLoginField = new JTextField(25);
	private JPasswordField coordinatorGmailPasswordField = new JPasswordField(25);
	private JButton okButton = new JButton();
	private JButton cancelButton = new JButton();
	
	protected GmailLoginDialog(KeyMasterGui keyMasterGui)
	{
		super(keyMasterGui, true);
		this.keyMasterGui = keyMasterGui;
		setTitle("Adminstrator Gmail Info");

		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		String iconLoc = "com/astrofizzbizz/stoneedgetunnel/files/gmail.jpg";
		URL resourceURL = loader.getResource(iconLoc);
        setIconImage(new ImageIcon(resourceURL).getImage());
		
		JPanel labelPanel = new JPanel();
		labelPanel.setLayout(new GridLayout(3,1));
		labelPanel.add(new JLabel("Coordinator Name "));
		labelPanel.add(new JLabel("Coordinator Gmail Account Name "));
		labelPanel.add(new JLabel("Coordinator Gmail Password "));
		
		JPanel textPanel = new JPanel();
		textPanel.setLayout(new GridLayout(3,1));
		textPanel.add(coordinatorNameField);
		textPanel.add(coordinatorGmailLoginField);
		textPanel.add(coordinatorGmailPasswordField);

		JPanel userInfoPanel = new JPanel();
		userInfoPanel.setLayout(new BoxLayout(userInfoPanel, BoxLayout.X_AXIS));
		userInfoPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""),BorderFactory.createEmptyBorder(5,5,5,5)));
		userInfoPanel.add(labelPanel);
		userInfoPanel.add(textPanel);
		
        okButton.addActionListener(new GmailLoginDialogActionListeners("okButton", this));
        cancelButton.addActionListener(new GmailLoginDialogActionListeners("cancelButton", this));
        
        okButton.setIcon(new ImageIcon(loader.getResource("com/astrofizzbizz/stoneedgetunnel/files/ok.jpg")));
        cancelButton.setIcon(new ImageIcon(loader.getResource("com/astrofizzbizz/stoneedgetunnel/files/cancel.jpg")));
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS));
		buttonPanel.add(okButton);
		buttonPanel.add(cancelButton);
		
		JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(""),BorderFactory.createEmptyBorder(5,5,5,5)));
        mainPanel.add(userInfoPanel);
        mainPanel.add(buttonPanel);
       
		getContentPane().add(mainPanel);
		
        pack();
        setLocationRelativeTo(keyMasterGui);
        setVisible(true);
	}
	public String getCoordinatorGmailLogin() {return coordinatorGmailLogin;}
	public String getCoordinatorName() {return coordinatorName;}
	public String getCoordinatorGmailPassword() {return coordinatorGmailPassword;}
	
	public void setCoordinatorGmailLogin(String coordinatorGmailLogin) {this.coordinatorGmailLogin = coordinatorGmailLogin;}
	public void setCoordinatorName(String coordinatorName) {this.coordinatorName = coordinatorName;}
	public void setCoordinatorGmailPassword(String coordinatorGmailPassword) {this.coordinatorGmailPassword = coordinatorGmailPassword;}
	
	private class GmailLoginDialogActionListeners implements ActionListener
	{
		String actionString = "";
		GmailLoginDialog gmailLoginDialog;
		private GmailLoginDialogActionListeners(String actionString, GmailLoginDialog gmailLoginDialog)
		{
			this.actionString = actionString;
			this.gmailLoginDialog = gmailLoginDialog;
		}

		@Override
		public void actionPerformed(ActionEvent arg0) 
		{
			if (actionString.equals("okButton"))
			{
				if (FileReaderUtilities.stripWhiteSpaces(coordinatorGmailLoginField.getText()).length() > 2)
				{
					setCoordinatorGmailLogin(FileReaderUtilities.stripWhiteSpaces(coordinatorGmailLoginField.getText()));
				}
				else
				{
					DpmSwingUtilities.messageDialog("Bad Gmail Login Name!", gmailLoginDialog);
					setCoordinatorGmailLogin(null);
					setCoordinatorName(null);
					setCoordinatorGmailPassword(null);
				}
				if (FileReaderUtilities.stripWhiteSpaces(coordinatorNameField.getText()).length() > 2)
				{
					setCoordinatorName(coordinatorNameField.getText());
				}
				else
				{
					DpmSwingUtilities.messageDialog("Bad Coordinator Name!", gmailLoginDialog);
					setCoordinatorGmailLogin(null);
					setCoordinatorName(null);
					setCoordinatorGmailPassword(null);
				}
				FileReaderUtilities.stripWhiteSpaces(new String(coordinatorGmailPasswordField.getPassword()));
				if (FileReaderUtilities.stripWhiteSpaces(new String(coordinatorGmailPasswordField.getPassword())).length() > 2)
				{
					setCoordinatorGmailPassword(FileReaderUtilities.stripWhiteSpaces(new String(coordinatorGmailPasswordField.getPassword())));
				}
				else
				{
					DpmSwingUtilities.messageDialog("Bad Password!", gmailLoginDialog);
					setCoordinatorGmailLogin(null);
					setCoordinatorName(null);
					setCoordinatorGmailPassword(null);
				}
				if (getCoordinatorGmailLogin() != null)
				{
					gmailLoginDialog.keyMasterGui.properties.setProperty("coordinatorGmailLogin", getCoordinatorGmailLogin());
					gmailLoginDialog.keyMasterGui.properties.setProperty("coordinatorName", getCoordinatorName());
					gmailLoginDialog.keyMasterGui.properties.setProperty("coordinatorGmailPassword", getCoordinatorGmailPassword());
					try {
						gmailLoginDialog.keyMasterGui.properties.storeToXML(new FileOutputStream(".stoneEdgeKeyMasterConfig.xml"), new Date().toString());
						gmailLoginDialog.keyMasterGui.statusBar.setText("Gmail Account Info Saved.");

					} catch (FileNotFoundException e) {
						DpmSwingUtilities.messageDialog("Cannot save config. file!" + e.getMessage(), gmailLoginDialog);
					} catch (IOException e) {
						DpmSwingUtilities.messageDialog("Cannot save config. file!" + e.getMessage(), gmailLoginDialog);
					}
				}
				gmailLoginDialog.setVisible(false);
			}
			if (actionString.equals("cancelButton"))
			{
				setCoordinatorGmailLogin(null);
				setCoordinatorName(null);
				gmailLoginDialog.setVisible(false);
			}
		}
		
	}

}
